package com.micromsg.basic.sys.service.impl;

import com.micromsg.common.constant.CommonEnum;
import com.micromsg.common.service.AbstractService;
import com.micromsg.common.core.PageResult;
import com.micromsg.common.util.IdGen;
import com.micromsg.common.util.ListUtil;
import com.micromsg.common.constant.CommonConstant;
import com.micromsg.basic.sys.dao.MessageMapper;
import com.micromsg.basic.sys.model.Message;
import com.micromsg.basic.sys.service.MessageService;
import com.micromsg.basic.sys.vo.entity.MessageEntityVO;
import com.micromsg.basic.sys.vo.request.MessageRequestVO;
import com.micromsg.basic.sys.vo.response.MessageResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 消息表
 *
 * @author yangyang
 * @date 2024/04/06
 **/
@Slf4j
@Service
@Transactional
public class MessageServiceImpl extends AbstractService<Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 获取消息表分页列表
     *
     * @param messageRequestVO 消息表查询条件
     * @return pageResult 消息表分页结果
     */
    @Override
    public PageResult<MessageResponseVO> queryMessagePageList(MessageRequestVO messageRequestVO) {
        //
        if (messageRequestVO.getPageNumber() == null) messageRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (messageRequestVO.getPageSize() == null) messageRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<MessageResponseVO> messageResponseVOList = Collections.emptyList();
        Condition condition = new Condition(Message.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(messageRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + messageRequestVO.getDesc() + "%");
        //}
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(messageRequestVO.getPageNumber(), messageRequestVO.getPageSize());
            List<Message> messageList = this.selectByCondition(condition);
            messageResponseVOList = ListUtil.convertList(messageList, MessageResponseVO.class);
        }
        //
        return PageResult.of(total, messageResponseVOList);
    }

    /**
     * 获取消息表详细信息
     *
     * @param id 消息表ID
     * @return 消息表
     */
    @Override
    public MessageEntityVO getMessageEntityVO(String id) {
        Message message = this.selectByPrimaryKey(id);
        if (null == message) return null;

        MessageEntityVO messageEntityVO = new MessageEntityVO();
        BeanUtils.copyProperties(message, messageEntityVO);
        return messageEntityVO;
    }

    /**
     * 添加消息表
     *
     * @param messageEntityVO 消息表
     */
    @Override
    public void addMessage(MessageEntityVO messageEntityVO) {
        Message message = new Message();
        BeanUtils.copyProperties(messageEntityVO, message);
        message.setId(IdGen.next());
        this.insertSelective(message);
    }

    /**
     * 修改消息表
     *
     * @param messageEntityVO 消息表
     */
    @Override
    public void updateMessage(MessageEntityVO messageEntityVO) {
        Message message = new Message();
        BeanUtils.copyProperties(messageEntityVO, message);
        this.updateByPrimaryKeySelective(message);
    }


    /**
     * 删除消息表
     *
     * @param id 消息表ID
     */
    @Override
    public void deleteMessage(String id) {
        this.deleteByPrimaryKey(id);
    }
}
